package com.xmtlxx.sacw.service.impl;

import com.xmtlxx.common.core.domain.PageQuery;
import com.xmtlxx.common.core.page.TableDataInfo;
import com.xmtlxx.common.helper.LoginHelper;
import com.xmtlxx.common.utils.DateUtils;
import com.xmtlxx.common.utils.StringUtils;
import com.xmtlxx.sacw.domain.EvaluateInfo;
import com.xmtlxx.sacw.domain.EvaluatePro;
import com.xmtlxx.sacw.domain.vo.EvaluateInfoVo;
import com.xmtlxx.sacw.mapper.EvaluateInfoMapper;
import com.xmtlxx.sacw.mapper.EvaluateProMapper;
import com.xmtlxx.sacw.service.IEvaluateService;
import com.xmtlxx.system.domain.SysOss;
import com.xmtlxx.system.domain.bo.SysOssBo;
import com.xmtlxx.system.enums.OssBuss;
import com.xmtlxx.system.mapper.SysOssMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 评估Service业务层处理
 *
 * @author hergreen
 * @date 2024/07/19 10:37
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class EvaluateServiceImpl implements IEvaluateService {

    private final EvaluateInfoMapper evaluateInfoMapper;

    private final EvaluateProMapper evaluateProMapper;

    private final SysOssMapper sysOssMapper;

    @Override
    public TableDataInfo<EvaluateInfoVo> queryPageList(EvaluateInfo bo, PageQuery pageQuery) {

        Page<EvaluateInfoVo> result = evaluateInfoMapper.selectPageEvaluateList(pageQuery.build(), bo);
        return TableDataInfo.build(result);
    }

    @Override
    public List<EvaluateInfoVo> queryList(EvaluateInfo bo) {

        return evaluateInfoMapper.selectEvaluateList(bo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean insert(EvaluateInfo info) {

        //设置评估编号
        info.setEvaluateNo(getNo());

        //设置部门Id
        info.setDeptId(LoginHelper.getDeptId());

        if (evaluateInfoMapper.insert(info) == 0) {
            throw new RuntimeException("新增评估信息失败");
        }

        info.getEvaluateProList().forEach(evaluatePro -> {
            evaluatePro.setEvaluateId(info.getId());
        });

        if (!evaluateProMapper.insertBatch(info.getEvaluateProList())) {
            throw new RuntimeException("新增评估财物失败");
        }

        //update oss info
        sysOssMapper.updateBatchBussAndObjIdById(info.getOss(), OssBuss.EVALUATE, info.getId());

        return true;
    }

    @Override
    public EvaluateInfo getInfo(String id) {

        EvaluateInfo evaluateInfo = evaluateInfoMapper.selectById(id);

        //when info is not null
        if (evaluateInfo != null) {

            //add evaluate property
            List<EvaluatePro> list = evaluateProMapper.getListByEvaluateId(id);

            evaluateInfo.setEvaluateProList(list);

            //add evaluate attachment
            List<SysOss> sysOssList = sysOssMapper.selectSysOssList(new SysOssBo(id, OssBuss.EVALUATE.getValue()));

            evaluateInfo.setOss(sysOssList);
        }

        return evaluateInfo;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean update(EvaluateInfo info) {

        evaluateInfoMapper.updateById(info);

        info.getEvaluateProList().forEach(evaluatePro -> {
            evaluatePro.setEvaluateId(info.getId());
        });

        evaluateProMapper.insertOrUpdateBatch(info.getEvaluateProList());

        info.getOss().forEach(oss -> {
            oss.setObjid(info.getId());
            oss.setBuss(OssBuss.EVALUATE.getValue());
        });

        sysOssMapper.insertOrUpdateBatch(info.getOss());

        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean delete(String id) {

        if (!evaluateInfoMapper.updateDelById(id)) {
            throw new RuntimeException("删除评估信息失败");
        }

        if (!evaluateProMapper.updateDelByEvaluateId(id)) {
            throw new RuntimeException("删除评估财物失败");
        }

        return true;
    }

    public String getNo() {

        Long count = evaluateInfoMapper.selectCount(Wrappers.lambdaQuery());
        String num = "";
        String todayStr = DateUtils.date2String(new Date(), "yyMMdd");

        if (count == null) {
            num = todayStr + "000001";
        } else {
            String maxnumStr = "00000" + count.toString();
            String maxnumNo = maxnumStr.substring(maxnumStr.length() - 6, maxnumStr.length());
            String nextnumNo = StringUtils.nextNo(maxnumNo, 6);
            num = todayStr + nextnumNo;
        }
        return num;
    }

}
